<?php
namespace app\api\controller;
use app\common\BaseController;
use app\api\model\Manager as ManagerModel;
use app\common\Logs;
class Manager extends BaseController{
    public function __construct(ManagerModel $managerModel){
        parent::__construct();
        $this->model = $managerModel;
    }
    public function allList(){
        $params = $this->request->param();
        $data = $this->model->select();
        return $data;
    }
    public function keywordAll(){
        $params = $this->request->param();
        $where = [];      
        if(!empty($params)){            
            //模糊查询           
            if(!empty($filter['keyword'])){
                $where['user_name|cn_name'] = ['like',"%{$params['keyword']}%"];               
            }  
        }       
        $data = $this->model->where($where)->order('id', 'desc')->select();
        return $data;
    }
    public function index(){
        $params = $this->request->param();
        $data = $this->model->allowField(true)->managerList($params);
        return $data;
    }
    //添加
    public function create(){
        $param = $this->request->param();       
        $params = json_decode($param['params'],true);        
        $operator_id = json_decode($param['operator_id'],true);    
        $params['password'] = md5(md5($params['password']));
        $data = $this->model->allowField(true)->data($params)->save();
        if($data){
            Logs::add("新增用户记录",$operator_id,$this->model->cn_name, null,json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        }
        return $this->model;
    }
    //更新
    public function update(){
        $param = $this->request->param();       
        $params = json_decode($param['params'],true);        
        $operator_id = json_decode($param['operator_id'],true); 
        $old = $this->model->where("id",$params['id'])->find();     
        $data = $this->model->allowField(true)->isUpdate(true)->save($params);
        if($data){
            Logs::add("修改用户记录",$operator_id,$old['cn_name'], json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        } 
        return $this->model;
    }
    //删除
    public function delete(){
        $params = $this->request->param();
        $old = $this->model->where("id",$params['id'])->find();   
        $data = $this->model->allowField(true)->where('id',$params['id'])->delete();
        if($data){
            Logs::add("删除用户记录",$params['operator_id'],$old['cn_name'], json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        } 
        return $data;
    }
    //锁定
    public function lock(){
        $params = $this->request->param();        
        $data=$this->model->where('id',$params["id"])->update(['lock'=>$params['num']]);  
        return $data;
    }   
}
